<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" plain @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <div class="toggle-button" @click="showHiddle">|||</div>
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级导航 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 标题 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级导航 -->
            <el-menu-item :index="'/' + sonItem.path" v-for="sonItem in item.children" :key="sonItem.id" @click="setActivepath('/' + sonItem.path)">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 标题 -->
              <span>{{sonItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  // 设置生命周期函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出功能
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧分类数据
    async getMenuList () {
      var { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$msg.error('分类数据获取失败')
      this.menulist = res.data
    },
    // 是否隐藏侧边栏
    showHiddle () {
      this.isCollapse = !this.isCollapse
    },
    // 左侧导航栏高亮
    setActivepath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 16px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu{
    border-right: none;
  }
}

.el-main {
  background-color: #eee;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.iconfont {
  margin-right: 10px;
}
</style>
